local Tower = MClass("Tower")
local ColorBlue = cc.c3b(64, 233, 249)
local ColorRed = cc.c3b(253, 103, 63)

function Tower:ctor()
    --self.root 默认为根节点
    --设置事件
    display.setCascadeColorEnabled(self.root,true)
    self.mySide = {}
    self.mySidePanel = self:findChild("Panel")
    self.mySide[Enums.buildingType.base] = self.mySidePanel:findChild("base")
    self.mySide[Enums.buildingType.tower] = self.mySidePanel:findChild("tower")
    self.mySide["ruin"] = self.mySidePanel:findChild("ruin")
    self.mySide["spring"] = self.mySidePanel:findChild("spring")

    self.otherSide = {}
    self.otherSidePanel = self:findChild("Panel_b")

    self.otherSide[Enums.buildingType.base] = self.otherSidePanel:findChild("base")
    self.otherSide[Enums.buildingType.tower] = self.otherSidePanel:findChild("tower")
    self.otherSide["ruin"] = self.otherSidePanel:findChild("ruin")
    self.otherSide["spring"] = self.otherSidePanel:findChild("spring")

	self.labelHpPercent = self:findChild("num")

    self:init()
end

function Tower:init()
	for _,building in pairs(self.mySide) do
		building:setVisible(false)
	end
	for _,building in pairs(self.otherSide) do
		building:setVisible(false)
	end
end

function Tower:setBuildingType(buildingType,bEnemy,bNeutral)
	self.buildingType = buildingType
	self.labelHpPercent:setString("100%")
	if bEnemy then
		self.otherSidePanel:setVisible(true)
		self.mySidePanel:setVisible(false)
		self.side = self.otherSide
		self.labelHpPercent:setColor(ColorRed)
		self.labelHpPercent:setVisible(false)
	else
		self.otherSidePanel:setVisible(false)
		self.mySidePanel:setVisible(true)
		self.side = self.mySide
		self.labelHpPercent:setColor(ColorBlue)
		self.labelHpPercent:setVisible(false)
	end

	if buildingType == Enums.buildingType.base then
		self.side[buildingType]:setVisible(true)
	elseif buildingType == Enums.buildingType.tower then
		self.side[buildingType]:setVisible(true)
	elseif buildingType == Enums.buildingType.mine1 then
	elseif buildingType == Enums.buildingType.mine2 then
	end
end

function Tower:setRuin(bRuin)
	if bRuin and self.buildingType == Enums.buildingType.tower  then
		self.side[self.buildingType]:setVisible(false)
		self.side["ruin"]:setVisible(true)
	end
end

function Tower:setHpPercent(hpPercent)
	hpPercent = math.ceil(hpPercent * 100)
	local strHpPercent = string.format("%d%%", hpPercent)
	self.labelHpPercent:setString(strHpPercent)
end

function Tower:setHpVisible(visible)
	self.labelHpPercent:setVisible(visible)
end

return Tower